#include "include.h"
#include <ESP8266HTTPClient.h>
HTTPClient http;

void http_post_request(String msg){
    String url = "http://" + http_config_parameter.server_ip + "/" + http_config_parameter.parameter + "/";
    http.begin(client, url); //HTTP
    http.addHeader("Content-Type", "application/json");

    int httpCode = http.POST(msg);
    if (httpCode > 0) {
        if (httpCode == HTTP_CODE_OK) {
            http_config_parameter.http_status = true;
            const String& payload = http.getString();
            if(payload != null){
                /*  将数据放在，缓冲区中*/
                system_status.client_CallBack_msg = payload;
                // serial_println(payload);
            }
        }
        else {
            http_config_parameter.http_status = false;
            /*  用户输出*/
            user_serial_print("error-parameter\r\n");
        }
        http.end();
    }
}

void http_get_request(void){
    String url = "http://" + http_config_parameter.server_ip + "/" + http_config_parameter.parameter + "/";
    http.begin(client, url); //HTTP
    int httpCode = http.GET();
    if (httpCode > 0) {
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
            http_config_parameter.http_status = true;
            const String& payload = http.getString();
            if(payload != null){
                /*  将数据放在，缓冲区中*/
                system_status.client_CallBack_msg = payload;
                // serial_println(payload);
            }
        }
        else {
            http_config_parameter.http_status = false;
            /*  用户输出*/
            user_serial_print("error-parameter\r\n");
        }
        http.end();
    }
}

// secretkey
String http_post_query_secret(String msg){
    String url = "http://www.caisiyu.top:1880/secretkey/";
    http.begin(client, url); //HTTP
    http.addHeader("Content-Type", "application/json");
    int httpCode = http.POST(msg);
    if (httpCode > 0) {
        /*  用户输出*/
        if (httpCode == HTTP_CODE_OK) {
            const String& payload = http.getString();
            if(payload != null){
                // serial_println(payload);
                if(payload.toInt() == 1){
                    system_status.secret_key_value = true;
                    /*  用户输出*/
                    user_serial_print("Ok\r\n");
                }
                else if(payload.toInt() == 0){
                    system_status.secret_key_value = false;
                    /*  用户输出*/
                    user_serial_print("error-secret\r\n");
                }
            }
        }
        else {
            system_status.secret_key_value = false;
            /*  用户输出*/
            user_serial_print("error-secret\r\n");
        }
        http.end();
    }
    return "";
}